package com.vue.vuedemo.Controller;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.vue.vuedemo.pojo.Chat;
import com.vue.vuedemo.pojo.Friend;
import com.vue.vuedemo.service.ChatService;
import com.vue.vuedemo.service.FriendService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
@JsonIgnoreProperties({"hibernateLazyInitializer","handler"})
@CrossOrigin(origins = {"http://localhost:8080"}) //当前网站向另一个网站请求数据就是跨域
@Controller
//调用业务接受前端参数
public class ChatController {

    @Resource
    private ChatService chatService;

    @GetMapping(value = "chatFindAll")
    public List<Chat> findAll(Integer cFpid,  Integer cJpid){
        chatService.chatUpdateType(cJpid);
        List<Chat> list = chatService.findAll(cFpid, cJpid);
        return list;
    }

    Integer num;

    //发送消息
    @GetMapping(value = "chatAdd")
    public Integer commentAdd(Integer cFpid,Integer cJpid ,String cContext,Chat chat){
            chat.setCFpid(cFpid);
            chat.setCJpid(cJpid);
            chat.setCContext(cContext);
        try {
            chatService.add(chat);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
            System.out.print(ex);
        }
        return num;
    }


    //删除聊天
    @GetMapping(value = "chatDel")
    public Integer del(Integer cId){
        try {
            chatService.del(cId);
            num = 1; //成功
        }catch (Exception ex){
            num = 2; //失败
        }
        return num;
    }

    @GetMapping(value = "chatCountFindAll")
    public Integer chatCountFindAll(Integer cJpid){
        Integer number = chatService.countChat(cJpid);
        return number;
    }

    //修改消息状态
//    @GetMapping(value = "chatUpdateType")
//    public Integer chatUpdateType(Integer cId){
//        try {
//            chatService.del(cId);
//            num = 1; //成功
//        }catch (Exception ex){
//            num = 2; //失败
//        }
//        return num;
//    }
}
